﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Backend.JYD.Model;
using Backend.JYD.BLL;
using Backend.JYD.Business;
using Backend.JYD.Model.Auth;

namespace Backend.JYD.RunTime
{
    public class BaseController : AbController
    {
        public const int PageSize20 = 20;
        public const int PageSize50 = 50;
        protected RoleManager roleManager = new RoleManager();
        protected AuthManager authManager = new AuthManager();

        #region 员工相关前置数据

        /// <summary>
        /// 员工ID
        /// </summary>
        protected Int64 EmpId { get; private set; }
        /// <summary>
        /// 员工当前角色ID
        /// </summary>
        protected List<Int64> RoleIds
        {
            get
            {
                if (this.LoginRoleInfos == null) return null;
                return this.LoginRoleInfos.Select(p => p.RoleId).ToList();
            }
        }
      

        #endregion
 

        //不存在的action
        protected override void HandleUnknownAction(string actionName)
        {
            base.HandleUnknownAction(actionName);
        }
        //不存在的控制器
        protected override HttpNotFoundResult HttpNotFound(string statusDescription)
        {
            return base.HttpNotFound(statusDescription);
        }
        //授权，权限控制
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
        }

        //预处理请求
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //更新session
            //this.EmpId = 2;
            //this.LoginEmpInfo = new EmpInfo() { EmpId = this.EmpId, EmpName = "测试用户" };

            //this.EmpId = 3;
            //this.LoginEmpInfo = new EmpInfo() { EmpId = this.EmpId, EmpName = "测试用户3" };
            //this.LoginEmpRoleInfos = new RoleInfoBLL().GetEmpRoleInfoByEmpId(this.EmpId);

            //var empInfo = SessionMgr.GetEmpInfoFromSession(this.HttpContext);

            base.OnActionExecuting(filterContext);

            if (this.LoginEmpInfo != null)
            {
                this.EmpId = this.LoginEmpInfo.EmpId;
            }
            else
            {
                filterContext.Result = new RedirectResult("http://" + filterContext.RequestContext.HttpContext.Request.Url.Host);
                return;
            }

            this.ViewBag.EmpId = this.EmpId;

        }

    }
}
